$(function ()
{

    //设置指定面板展开
    $('#layout').layout('expand', 'north');
    $('#showSensorInfo').window('close');
    //设置开始时间
    setStartTime();
    //从url中获取参数
    var Request = getUrlRequest();
    var orderDetail = JSON.parse(Request["orderDetail"]);
    //获取primarykey
    var primarykey = orderDetail.primarykey;
    var labcode=orderDetail.labcode;
    var itemcontent1=orderDetail.itemcontent1;
    var itemcontent2=orderDetail.itemcontent2;

    var datagrid;
    datagrid = $("#dg").datagrid(
        {
           url : "../../api/cs_getAllTestData?primarykey="+primarykey,
            //isField : "testnum",
            method : "get",
            width : 1000,
            height : 550,
            rownumbers : true, //显示行号
            singleSelect : true,//只支持单选
            toolbar : '#tc',//设置工具栏的id
            pagination : true, //显示分页
            pageSize : 10, //分页大小
            pageList : [5, 10, 15, 20], //每页的个数
            beforePageText : '第',
            afterPageText : '页    共 {pages} 页',
            displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            fit : true, //自动补全
            fitColumns : true,
            title : "",
            columns : [[//每个列具体内容
                    {
                        field : 'ck',
                        checkbox : true
                    },
                    {
                        field : 'labcode',
                        title : '实验室名称',
                        width : 100,
                        align : 'center',
                        formatter:function(val,row){
                            return getLabNameByLabCode(labcode);
                        }
                    },
                    {
                        field : 'itemcontent1',
                        title : '实验编号',
                        width : 150,
                        align : 'center',
                        formatter:function(val,row){
                            val=itemcontent1;
                            return val;
                        }
                    },
                    {
                        field : 'primarykey',
                        title : '开测时间',
                        width : 100,
                        align : 'center',
                        formatter:function(val,row){
                            val=row.primarykey;
                            val=val.substring(0,val.length-1);
                            return val;
                        }
                    },
                    {
                        field : 'time',
                        title : '测试时间',
                        width : 100,
                        align : 'center',
                        formatter:function(val,row){
                            val=row.date+" "+row.time;
                            return val;
                        }
                    },
                    {
                        field : 'testunitno',
                        title : '测试台位',
                        width : 50,
                        align : 'center'
                    },
                    {
                        field : 'howlong',
                        title : '测试时长',
                        width : 100,
                        align : 'center',
                        formatter:function(val,row){
                            return Math.round(val*3600)+"秒";
                        }
                    },
                    {
                        field : 'sensorvalue',
                        title : '传感器具体数据',
                        width : 130,
                        align : 'center',
                        formatter:function(value, row, index)
                        {
                            //创建一个对象，用于传递数据
                            function createSensorInfo(){
                                var o=new Object();
                                o.sensorNameList=row.sensorNameList;
                                o.sensorValueList=row.sensorValueList;
                                return o;
                            }
                           var SensorInfo=new createSensorInfo();
                           var info = JSON.stringify(SensorInfo);
                           return '<a href="#" onclick="ShowSensorInfo('+info.replace(/"/g, '&quot;')+');return false;" style=color:green; >传感器具体数据</a>' ; 
                        }
                    },
                ]],
            toolbar : [//工具条
                {
                    text : "传感器具体数据",
                    iconCls : "icon-redo",
                    handler : function ()
                    {

                        var rows = datagrid.datagrid('getSelections');

                        if (rows.length <= 0)
                        {
                            $.messager.alert('警告', '您没有选择', 'error');
                        }
                        else if (rows.length > 1)
                        {
                            $.messager.alert('警告', '不支持批量接收', 'error');
                        }
                        else
                        {
                            //创建一个对象，用于传递数据
                            function createSensorInfo(){
                                var o=new Object();
                                o.sensorNameList=rows[0].sensorNameList;
                                o.sensorValueList=rows[0].sensorValueList;
                                return o;
                            }
                            var SensorInfo=new createSensorInfo();
                            ShowSensorInfo(SensorInfo);
                        }
                    }
                }

            ],
            onAfterEdit : function (rowIndex, rowData, changes)
            {
                var inserted = datagrid.datagrid('getChanges', 'inserted');
                var updated = datagrid.datagrid('getChanges', 'updated');
                if (inserted.length < 1 && updated.length < 1)
                {
                    editRow = undefined;
                    datagrid.datagrid('unselectAll');
                    return;
                }

                var url = '';
                $.messager.alert("信息", inserted.length, "info");
                if (inserted.length > 0)
                {
                    url = 'insertsampleinfo.do';
                }

                if (updated.length > 0)
                {
                    url = 'updateSampleInfo.do';
                }
                $.ajax(
                {
                    url : url,
                    data : rowData,
                    dataType : 'json',
                    success : function (r)
                    {
                        if (r.success)
                        {
                            datagrid.datagrid('acceptChanges');
                            $.messager.show(
                            {
                                msg : r.msg,
                                title : '成功'
                            }
                            );
                            editRow = undefined;
                            datagrid.datagrid('reload');
                        }
                        else
                        {
                            /*datagrid.datagrid('rejectChanges');*/
                            datagrid.datagrid('beginEdit', editRow);
                            $.messager.alert('错误', r.msg, 'error');
                        }
                        datagrid.datagrid('unselectAll');
                    }
                }
                );

            },
            onSelect : function (index, row)
            {
                console.log(index);
            },
        }
        );

}
);

//整体模糊查询
function searchTemporarySam()
{
    var orderno = $('#orderno').textbox('getValue');
    var samplename = $('#samplename').combobox('getText');
    var sampletype = $('#sampletype').searchbox('getValue');
    var endtime = $('#endtime').datetimebox('getValue');
    //不同角色查询的订单信息不一样
    var searchurl = "";
    //reload形式加载模糊查询,成功
    $('#dg').datagrid('reload', searchurl);

}

//显示传感器具体数据
function ShowSensorInfo(SensorInfo)
{
     //遍历传感器名称
    var sensorInfoHtml="";
    for ( var i = 0; i <SensorInfo.sensorNameList.length; i++){
        var sensorName=SensorInfo.sensorNameList[i];
        var sensorValue=SensorInfo.sensorValueList[i];
        sensorInfoHtml+='<tr><td>'+sensorName+'</td><td>'+sensorValue+'</td></tr>'
    }
    //拼接html语句
    $("#sensorInfo").html(sensorInfoHtml);
    var xposition = screen.width / 2 - 400;
    $('#showSensorInfo').window('open').window('move', {
        left: xposition,
        top: 50,
      });

}



